package com.chess.chessplay;

import java.io.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class computer {

    static boolean nowIsPlayer = true;

    static int lastx = -123456789, lasty = -123456789;
    static boolean computerPlayChessSign = true;
    //<<<<<<<<<<<<<<<<<

    // 棋手编号 0 ~ 3 (包含3)
    static int piecesPlay = 0;
    //
    // 其他棋手的编号
    static int otherFirstPlayer = 1;
    static int otherSecondPlayer = 2;
    static int otherThirdPlayer = 3;

    // 全局变量
    static String piecesColor[] = {"r1", "y1", "b1", "g1"}; // 棋手所用棋子颜色标志
    static String notPosition[] = {"r2", "y2", "b2", "g2"}; // 棋手在该区域不能下棋子标志
    static String canPosition[] = {"r3", "y3", "b3", "g3"}; // 棋手可在该区域下棋子标志
    static String color[] = {"red", "yellow", "blue", "green"};
    static String[][][] con = new String[22][8][8]; //棋子需要用到的数组
    static String[][] boxIndex = new String[21][21]; // 棋盘需要用到的数组
    // 以最中间的一个格子为中心，上下左右各含有几个棋子
    static int[] leftNum = new int[22];
    static int[] rightNum = new int[22];
    static int[] upperNum = new int[22];
    static int[] lowerNum = new int[22];
    static boolean[] determine = new boolean[22]; // 标记哪些棋子被用了
    static int choose = 0;// 聚焦的棋子
    static boolean first = true; // 标记是否下的为第一步

    static boolean[][] comUsed = new boolean[4][22];

    //电脑1,2,3
    //let

//    // 屏幕的高和宽
//    let scWidth = screen.width;
//    let scHeight = screen.height;

//    // 旋转、翻转需要用到的参数
//    let angle1 = -1;
//    let angle2 = 90;
//    let angle3 = -1;

    // 棋盘参数
    static int boxLength = 800;
    static int gridLength = 40;

    //存放答案的文件的路径
    static String file_path = "E:\\java\\Test";
    //存放答案的的文件名称
    static String file_name = "answer.txt";



    //复盘
    static String[][][] replay = new String[100][21][21];
    static int rnum = 0;

    public static void main(String[] args) {
        //初始化电脑的棋子使用情况
        initComputerUsed();
        //websocket();
        // 初始化棋盘
        initChessBoard();
        // 初始化各玩家棋子参数
        initChessPlay();
        // 在候选区画出棋子
//        drawChess();
        // 为每个在候选区的棋子添加点击事件，使其可以显示在展示区
//        candChessClick();
        // 展示区内的棋子添加操作
//        chessChangeOperating();

        //打印boxIndex
//        printBoxIndex();

        move();
        display();
    }
    //展示
    private static void display() {
        String s;
//        for(int m = 0; m < rnum; m++){
//            for(int i = 0; i < replay[m].length; i++){
//                for (int j = 0; j < replay[m][i].length; j++){
//                    System.out.print(replay[m][i][j]);
//                }
//                System.out.println(",");
//            }
//            System.out.println("\n");
//        }
        s = convert(replay);
        writeFileContent(file_path+"\\"+file_name, s);
        String answer = readFileContent(file_path+"\\"+file_name);
        System.out.println(answer);
    }
    //读取指定目录文件中的数据
    public static String readFileContent(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                sbf.append(tempStr);
            }
            reader.close();
            return sbf.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return sbf.toString();
    }
    //将字符串写入指定目录文件
    public static void writeFileContent(String fileName, String content){

        try {
            File file = new File(fileName);
            // if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }
            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //把数组转换成字符串
    private static String convert(String[][][] chess){
        String m = "";
        for(int i = 0; i < rnum; i++){
            if(i != 0){
                m += "|";
            }
            for (int j = 0; j < chess[i].length; j++){
                if(j != 0){
                    m+="#";
                }
                for (int x = 0; x < chess[i][j].length; x++){
                    if(x != 0){
                        m+=",";
                    }
                    m += chess[i][j][x];
                }
            }
        }
        return m;
    }

    //复盘记录数据
    private static void myreplay(String[][] chess) {
        for(int i = 0; i < chess.length; i++){
            for(int j = 0; j < chess[i].length; j++){
                replay[rnum][i][j] = chess[i][j];
            }
        }
        rnum++;

    }

    //移动，调用运算函数
    private static void move() {
        int j = 0;
        int[] a = new int[4];
        for(int i = 0; i < 4; i++){
            a[i] = 0;
        }
        int i = 0;
        while(true){

            if(i == 0){//玩家一
                if(a[i] != 1){
                    a = computerPlay(i,a);
                }
                if(a[i] != 1){
                    //将结果写进数组
                    myreplay(boxIndex);
                }
            }else if(i == 1){//玩家二
                if(a[i] != 1){
                    a = computerPlay(i,a);
                }
                if(a[i] != 1){
                    //将结果写进数组
                    myreplay(boxIndex);
                }
            }else if(i == 2){//玩家三
                if(a[i] != 1){
                    a = computerPlay(i,a);
                }
                if(a[i] != 1){
                    //将结果写进数组
                    myreplay(boxIndex);
                }
            }else if (i == 3){//玩家四
                if(a[i] != 1){
                    a = computerPlay(i,a);
                }
                if(a[i] != 1){
                    //将结果写进数组
                    myreplay(boxIndex);
                }
            }
            i++;
            i %= 4;
//            for (int i = 0; i < 4; i++){
//                if(a[i] != 1){
//                    a = computerPlay(i,a);
//                }
//                if(a[i] != 1){
//                    //将结果写进数组
//                    myreplay(boxIndex);
//                }
//            }
            j++;
            if(a[0] == 1 && a[1] == 1&& a[2] == 1 && a[3] == 1){
                break;
            }
        }
    }


    private static int[] Xuan90Y(int[] tempZ, int[] disCenterY) {
        int[] tempY = new int[5];
        for ( int i = 0; i < tempZ.length; ++i ) {
            if ( tempZ[i] == 0 ) {
                tempY[i] = tempZ[i];
            }
            else if ( disCenterY[i] == 0 ) {
                tempY[i] = -tempZ[i];
            }
            else {
                tempY[i] = -tempZ[i];
            }
        }
        return tempY;
    }

    private static int[] Xuan90X(int[] disCenterX, int[] disCenterY) {
        int[] tempX = new int[5];
        for ( int i = 0; i < disCenterX.length; ++i ) {
            tempX[i] = disCenterY[i];
        }
        return tempX;
    }

    private static int[] LeftAndRightY(int[] tempZ, int[] disCenterY) {
        int[] tempY = new int[5];
        for ( int i = 0; i < tempZ.length; ++i ) {
            if ( disCenterY[i] == 0 ) tempY[i] = disCenterY[i];
            else tempY[i] = -disCenterY[i];
        }
        return tempY;
    }

    private static int[] LeftAndRightX(int[] disCenterX, int[] disCenterY) {
        int[] tempX = new int[5];
        for ( int i = 0; i < disCenterX.length; ++i ) {
            tempX[i] = disCenterX[i];
        }
        return tempX;
    }

    private static void updateComBoxIndex(int no) {
        for ( int i = 1; i <= 20; ++i ) {
            for ( int j = 1; j <= 20; ++j ) {
                if ( boxIndex[i][j] == piecesColor[no] ) {
                    //斜对角方向
                    if ( i-1 >= 1 && i-1 <= 20 && j-1 >= 1 && j-1 <= 20 ) {
                        if ( boxIndex[i-1][j-1] == "0" ) { boxIndex[i-1][j-1] = canPosition[no];}
                    }
                    if ( i-1 >= 1 && i-1 <= 20 && j+1 >= 1 && j+1 <= 20 ) {
                        if ( boxIndex[i-1][j+1] == "0" ) { boxIndex[i-1][j+1] = canPosition[no];}
                    }
                    if ( i+1 >= 1 && i+1 <= 20 && j-1 >= 1 && j-1 <= 20 ) {
                        if ( boxIndex[i+1][j-1] == "0" ) { boxIndex[i+1][j-1] = canPosition[no];}
                    }
                    if ( i+1 >= 1 && i+1 <= 20 && j+1 >= 1 && j+1 <= 20 ) {
                        if ( boxIndex[i+1][j+1] == "0" ) { boxIndex[i+1][j+1] = canPosition[no];}
                    }
                }
            }
        }

        for ( int i = 1; i <= 20; ++i ) {
            for ( int j = 1; j <= 20; ++j ) {
                if ( boxIndex[i][j] == piecesColor[no] ) {
                    //竖直四个方向
                    if ( i+1 >= 1 && i+1 <= 20 ) {
                        if ( boxIndex[i+1][j] == "0" || boxIndex[i+1][j] == canPosition[no] ) { boxIndex[i+1][j] = notPosition[no];}
                    }
                    if ( i-1 >= 1 && i-1 <= 20 ) {
                        if ( boxIndex[i-1][j] == "0" || boxIndex[i-1][j] == canPosition[no] ) { boxIndex[i-1][j] = notPosition[no];}
                    }
                    if ( j-1 >= 1 && j-1 <= 20 ) {
                        if ( boxIndex[i][j-1] == "0" || boxIndex[i][j-1] == canPosition[no] ) { boxIndex[i][j-1] = notPosition[no];}
                    }
                    if ( j+1 >= 1 && j+1 <= 20 ) {
                        if ( boxIndex[i][j+1] == "0" || boxIndex[i][j+1] == canPosition[no] ) { boxIndex[i][j+1] = notPosition[no];}
                    }
                }
            }
        }

    }



    private static void initChessPlay() {
        for (int i = 0; i <= 21; i++) {
            leftNum[i] = 0;
            rightNum[i] = 0;
            upperNum[i] = 0;
            lowerNum[i] = 0;
            determine[i] = false;
            for (int j = 0; j < 8; j++) {
                for (int k = 0; k < 8; k++) {
                    con[i][j][k] = "0";
                }
            }
        }

        for (int i = 1; i <= 21; i++) {
            switch (i) {
                case 1:
                    con[i][4][4] = piecesColor[piecesPlay];
                    con[i][3][4] = con[i][4][3] = con[i][4][5] = con[i][5][4] = notPosition[piecesPlay];
                    con[i][3][3] = con[i][3][5] = con[i][5][3] = con[i][5][5] = canPosition[piecesPlay];
                    leftNum[i] = 0;
                    rightNum[i] = 0;
                    upperNum[i] = 0;
                    lowerNum[i] = 0;
                    break;
                case 2:
                    con[i][4][4] = con[i][4][5] = piecesColor[piecesPlay];
                    con[i][3][4] = con[i][3][5] = con[i][4][3] = con[i][4][6] = con[i][5][4] = con[i][5][5] = notPosition[piecesPlay];
                    con[i][3][3] = con[i][3][6] = con[i][5][3] = con[i][5][6] = canPosition[piecesPlay];
                    leftNum[i] = 0;
                    rightNum[i] = 1;
                    upperNum[i] = 0;
                    lowerNum[i] = 0;
                    break;
                case 3:
                    con[i][4][3] = con[i][4][4] = con[i][4][5] = piecesColor[piecesPlay];
                    con[i][3][3] = con[i][3][4] = con[i][3][5] = con[i][4][2] = con[i][4][6] = con[i][5][3] = con[i][5][4] = con[i][5][5] = notPosition[piecesPlay];
                    con[i][3][2] = con[i][3][6] = con[i][5][2] = con[i][5][6] = canPosition[piecesPlay];
                    leftNum[i] = 1;
                    rightNum[i] = 1;
                    upperNum[i] = 0;
                    lowerNum[i] = 0;
                    break;
                case 4:
                    con[i][3][4] = con[i][4][4] = con[i][4][5] = piecesColor[piecesPlay];
                    con[i][2][4] = con[i][3][3] = con[i][3][5] = con[i][4][3] = con[i][4][6] = con[i][5][4] = con[i][5][5] = notPosition[piecesPlay];
                    con[i][2][3] = con[i][2][5] = con[i][3][6] = con[i][5][3] = con[i][5][6] = canPosition[piecesPlay];
                    leftNum[i] = 0;
                    rightNum[i] = 1;
                    upperNum[i] = 1;
                    lowerNum[i] = 0;
                    break;
                case 5:
                    con[i][4][3] = con[i][4][4] = con[i][4][5] = con[i][4][6] = piecesColor[piecesPlay];
                    con[i][3][3] = con[i][3][4] = con[i][3][5] = con[i][3][6] = con[i][4][2] = con[i][4][7] = con[i][5][3] = con[i][5][4] = con[i][5][5] = con[i][5][6] = notPosition[piecesPlay];
                    con[i][3][2] = con[i][3][7] = con[i][5][2] = con[i][5][7] = canPosition[piecesPlay];
                    leftNum[i] = 1;
                    rightNum[i] = 2;
                    upperNum[i] = 0;
                    lowerNum[i] = 0;
                    break;
                case 6:
                    con[i][3][3] = con[i][4][3] = con[i][4][4] = con[i][4][5] = piecesColor[piecesPlay];
                    con[i][2][3] = con[i][3][2] = con[i][3][4] = con[i][3][5] = con[i][4][2] = con[i][4][6] = con[i][5][3] = con[i][5][4] = con[i][5][5] = notPosition[piecesPlay];
                    con[i][2][2] = con[i][2][4] = con[i][3][6] = con[i][5][2] = con[i][5][6] = canPosition[piecesPlay];
                    leftNum[i] = 1;
                    rightNum[i] = 1;
                    upperNum[i] = 1;
                    lowerNum[i] = 0;
                    break;
                case 7:
                    con[i][3][4] = con[i][4][3] = con[i][4][4] = con[i][4][5] = piecesColor[piecesPlay];
                    con[i][2][4] = con[i][3][3] = con[i][3][5] = con[i][4][2] = con[i][4][6] = con[i][5][3] = con[i][5][4] = con[i][5][5] = notPosition[piecesPlay];
                    con[i][2][3] = con[i][2][5] = con[i][3][2] = con[i][3][6] = con[i][5][2] = con[i][5][6] = canPosition[piecesPlay];
                    leftNum[i] = 1;
                    rightNum[i] = 1;
                    upperNum[i] = 1;
                    lowerNum[i] = 0;
                    break;
                case 8:
                    con[i][3][3] = con[i][3][4] = con[i][4][3] = con[i][4][4] = piecesColor[piecesPlay];
                    con[i][2][3] = con[i][2][4] = con[i][3][2] = con[i][3][5] = con[i][4][2] = con[i][4][5] = con[i][5][3] = con[i][5][4] = notPosition[piecesPlay];
                    con[i][2][2] = con[i][2][5] = con[i][5][2] = con[i][5][5] = canPosition[piecesPlay];
                    leftNum[i] = 1;
                    rightNum[i] = 0;
                    upperNum[i] = 1;
                    lowerNum[i] = 0;
                    break;
                case 9:
                    con[i][3][3] = con[i][3][4] = con[i][4][4] = con[i][4][5] = piecesColor[piecesPlay];
                    con[i][2][3] = con[i][2][4] = con[i][3][2] = con[i][3][5] = con[i][4][3] = con[i][4][6] = con[i][5][4] = con[i][5][5] = notPosition[piecesPlay];
                    con[i][2][2] = con[i][2][5] = con[i][3][6] = con[i][4][2] = con[i][5][3] = con[i][5][6] = canPosition[piecesPlay];
                    leftNum[i] = 1;
                    rightNum[i] = 1;
                    upperNum[i] = 1;
                    lowerNum[i] = 0;
                    break;
                case 10:
                    con[i][4][2] = con[i][4][3] = con[i][4][4] = con[i][4][5] = con[i][4][6] = piecesColor[piecesPlay];
                    con[i][3][2] = con[i][3][3] = con[i][3][4] = con[i][3][5] = con[i][3][6] = con[i][4][1] = con[i][4][7] = con[i][5][2] = con[i][5][3] = con[i][5][4] = con[i][5][5] = con[i][5][6] = notPosition[piecesPlay];
                    con[i][3][1] = con[i][3][7] = con[i][5][1] = con[i][5][7] = canPosition[piecesPlay];
                    leftNum[i] = 2;
                    rightNum[i] = 2;
                    upperNum[i] = 0;
                    lowerNum[i] = 0;
                    break;
                case 11:
                    con[i][3][3] = con[i][4][3] = con[i][4][4] = con[i][4][5] = con[i][4][6] = piecesColor[piecesPlay];
                    con[i][2][3] = con[i][3][2] = con[i][3][4] = con[i][3][5] = con[i][3][6] = con[i][4][2] = con[i][4][7] = con[i][5][3] = con[i][5][4] = con[i][5][5] = con[i][5][6] = notPosition[piecesPlay];
                    con[i][2][2] = con[i][2][4] = con[i][3][7] = con[i][5][2] = con[i][5][7] = canPosition[piecesPlay];
                    leftNum[i] = 1;
                    rightNum[i] = 2;
                    upperNum[i] = 1;
                    lowerNum[i] = 0;
                    break;
                case 12:
                    con[i][3][4] = con[i][4][3] = con[i][4][4] = con[i][4][5] = con[i][4][6] = piecesColor[piecesPlay];
                    con[i][2][4] = con[i][3][3] = con[i][3][5] = con[i][3][6] = con[i][4][2] = con[i][4][7] = con[i][5][3] = con[i][5][4] = con[i][5][5] = con[i][5][6] = notPosition[piecesPlay];
                    con[i][2][3] = con[i][2][5] = con[i][3][2] = con[i][3][7] = con[i][5][2] = con[i][5][7] = canPosition[piecesPlay];
                    leftNum[i] = 1;
                    rightNum[i] = 2;
                    upperNum[i] = 1;
                    lowerNum[i] = 0;
                    break;
                case 13:
                    con[i][3][4] = con[i][4][3] = con[i][4][4] = con[i][4][5] = con[i][5][4] = piecesColor[piecesPlay];
                    con[i][2][4] = con[i][3][3] = con[i][3][5] = con[i][4][2] = con[i][4][6] = con[i][5][3] = con[i][5][5] = con[i][6][4] = notPosition[piecesPlay];
                    con[i][2][3] = con[i][2][5] = con[i][3][2] = con[i][3][6] = con[i][5][2] = con[i][5][6] = con[i][6][3] = con[i][6][5] = canPosition[piecesPlay];
                    leftNum[i] = 1;
                    rightNum[i] = 1;
                    upperNum[i] = 1;
                    lowerNum[i] = 1;
                    break;
                case 14:
                    con[i][3][3] = con[i][3][4] = con[i][4][3] = con[i][4][4] = con[i][4][5] = piecesColor[piecesPlay];
                    con[i][2][3] = con[i][2][4] = con[i][3][2] = con[i][3][5] = con[i][4][2] = con[i][4][6] = con[i][5][3] = con[i][5][4] = con[i][5][5] = notPosition[piecesPlay];
                    con[i][2][2] = con[i][2][5] = con[i][3][6] = con[i][5][2] = con[i][5][6] = canPosition[piecesPlay];
                    leftNum[i] = 1;
                    rightNum[i] = 1;
                    upperNum[i] = 1;
                    lowerNum[i] = 0;
                    break;
                case 15:
                    con[i][3][3] = con[i][3][5] = con[i][4][3] = con[i][4][4] = con[i][4][5] = piecesColor[piecesPlay];
                    con[i][2][3] = con[i][2][5] = con[i][3][2] = con[i][3][4] = con[i][3][6] = con[i][4][2] = con[i][4][6] = con[i][5][3] = con[i][5][4] = con[i][5][5] = notPosition[piecesPlay];
                    con[i][2][2] = con[i][2][4] = con[i][2][6] = con[i][5][2] = con[i][5][6] = canPosition[piecesPlay];
                    leftNum[i] = 1;
                    rightNum[i] = 1;
                    upperNum[i] = 1;
                    lowerNum[i] = 0;
                    break;
                case 16:
                    con[i][3][4] = con[i][4][4] = con[i][5][3] = con[i][5][4] = con[i][5][5] = piecesColor[piecesPlay];
                    con[i][2][4] = con[i][3][3] = con[i][3][5] = con[i][4][3] = con[i][4][5] = con[i][5][2] = con[i][5][6] = con[i][6][3] = con[i][6][4] = con[i][6][5] = notPosition[piecesPlay];
                    con[i][2][3] = con[i][2][5] = con[i][4][2] = con[i][4][6] = con[i][6][2] = con[i][6][6] = canPosition[piecesPlay];
                    leftNum[i] = 1;
                    rightNum[i] = 1;
                    upperNum[i] = 1;
                    lowerNum[i] = 1;
                    break;
                case 17:
                    con[i][3][3] = con[i][4][3] = con[i][4][4] = con[i][4][5] = con[i][5][4] = piecesColor[piecesPlay];
                    con[i][2][3] = con[i][3][2] = con[i][3][4] = con[i][3][5] = con[i][4][2] = con[i][4][6] = con[i][5][3] = con[i][5][5] = con[i][6][4] = notPosition[piecesPlay];
                    con[i][2][2] = con[i][2][4] = con[i][3][6] = con[i][5][2] = con[i][5][6] = con[i][6][3] = con[i][6][5] = canPosition[piecesPlay];
                    leftNum[i] = 1;
                    rightNum[i] = 1;
                    upperNum[i] = 1;
                    lowerNum[i] = 1;
                    break;
                case 18:
                    con[i][3][3] = con[i][4][3] = con[i][4][4] = con[i][4][5] = con[i][5][5] = piecesColor[piecesPlay];
                    con[i][2][3] = con[i][3][2] = con[i][3][4] = con[i][3][5] = con[i][4][2] = con[i][4][6] = con[i][5][3] = con[i][5][4] = con[i][5][6] = con[i][6][5] = notPosition[piecesPlay];
                    con[i][2][2] = con[i][2][4] = con[i][3][6] = con[i][5][2] = canPosition[piecesPlay];
                    leftNum[i] = 1;
                    rightNum[i] = 1;
                    upperNum[i] = 1;
                    lowerNum[i] = 1;
                    break;
                case 19:
                    con[i][3][3] = con[i][4][3] = con[i][4][4] = con[i][5][4] = con[i][5][5] = piecesColor[piecesPlay];
                    con[i][2][3] = con[i][3][2] = con[i][3][4] = con[i][4][2] = con[i][4][5] = con[i][5][3] = con[i][5][6] = con[i][6][4] = con[i][6][5] = notPosition[piecesPlay];
                    con[i][2][2] = con[i][2][4] = con[i][3][5] = con[i][4][6] = con[i][5][2] = con[i][6][3] = con[i][6][6] = canPosition[piecesPlay];
                    leftNum[i] = 1;
                    rightNum[i] = 1;
                    upperNum[i] = 1;
                    lowerNum[i] = 1;
                    break;
                case 20:
                    con[i][3][3] = con[i][3][4] = con[i][4][4] = con[i][4][5] = con[i][4][6] = piecesColor[piecesPlay];
                    con[i][2][3] = con[i][2][4] = con[i][3][2] = con[i][3][5] = con[i][3][6] = con[i][4][3] = con[i][4][7] = con[i][5][4] = con[i][5][5] = con[i][5][6] = notPosition[piecesPlay];
                    con[i][2][2] = con[i][2][5] = con[i][3][7] = con[i][4][2] = con[i][5][3] = con[i][5][7] = canPosition[piecesPlay];
                    leftNum[i] = 1;
                    rightNum[i] = 2;
                    upperNum[i] = 1;
                    lowerNum[i] = 0;
                    break;
                case 21:
                    con[i][2][4] = con[i][3][4] = con[i][4][4] = con[i][4][5] = con[i][4][6] = piecesColor[piecesPlay];
                    con[i][1][4] = con[i][2][3] = con[i][2][5] = con[i][3][3] = con[i][3][5] = con[i][3][6] = con[i][4][3] = con[i][4][7] = con[i][5][4] = con[i][5][5] = con[i][5][6] = notPosition[piecesPlay];
                    con[i][1][3] = con[i][1][5] = con[i][3][7] = con[i][5][3] = con[i][5][7] = canPosition[piecesPlay];
                    leftNum[i] = 0;
                    rightNum[i] = 2;
                    upperNum[i] = 2;
                    lowerNum[i] = 0;
                    break;
            }
        }
    }

    private static void initChessBoard() {
        for (int i = 0; i < 21; i++) {
            for (int j = 0; j < 21; j++) {
                boxIndex[i][j] = "0";
            }
        }
        boxIndex[20][1] = "r3";
        boxIndex[20][20] = "y3";
        boxIndex[1][20] = "b3";
        boxIndex[1][1] = "g3";
    }

    private static void initComputerUsed() {
        for ( int i = 0; i <= 3; ++i ) {
            for ( int j = 0; j <= 21; ++j ) {
                comUsed[i][j] = false;
            }
        }
    }
    //下棋  a数组代表是否能继续下，如果能继续下棋，那么返回的数组a就不进行修改，如果不能下了，那么就将a[no]的值改为0
    private static int[] computerPlay(int no, int[] a) {
        updateComBoxIndex(no);  //更新棋盘，更新xxx2 xxx3的点

        boolean flag = false;
        Map<Integer, Integer> comCanLet = new HashMap<Integer, Integer>();//电脑编号no能下的位置
        //找位置
        for ( int i = 1; i <= 20; ++i ) {
            for (int j = 1; j <= 20; ++j ) {
                if ( boxIndex[i][j] == canPosition[no]) {
                    //console.log(i,j);
                    comCanLet.put(i, j);
//                    comCanLet.push({x:i,y:j});
                }
            }
        }
        //排序
        List<Map.Entry<Integer,Integer>> list = new ArrayList<Map.Entry<Integer,Integer>>(comCanLet.entrySet());
        Collections.sort(list,new Comparator<Map.Entry<Integer,Integer>>() {
            //升序排序
            public int compare(Map.Entry<Integer, Integer> a,
                               Map.Entry<Integer, Integer> b) {
                int a1 = Math.abs(a.getKey() - a.getValue());
                int a2 = Math.abs(b.getKey() - b.getValue());
                if ( a1 > a2 ) {
                    return 1;
                }
                else if ( a1 < a2 ) {
                    return -1;
                }
                else return 0;
            }

        });
        // console.log(comCanLet);
        int ComChoose;//电脑选择棋子的编号
        //按位置逐一枚举
        for ( int i = 0; i < list.size(); ++i ) {
            int x = list.get(i).getKey(), y = list.get(i).getValue();
            for ( int comi = 0; comi < 6; ++ comi ) {
                int[] DisCenterX = new int[5];
                int[] DisCenterY = new int[5];;

                ComChoose = (int)(Math.floor(10 + Math.random() * (21 - 10 + 1)));
                //console.log('电脑选择的棋子' + ComChoose);

                if ( comUsed[no][ComChoose] == true ) {
                    continue;
                }

                switch(ComChoose) {
                    case 10:
                        DisCenterX[0] = 0;DisCenterY[0] = 0;
                        DisCenterX[1] = 0;DisCenterY[1] = 1;
                        DisCenterX[2] = 0;DisCenterY[2] = 2;
                        DisCenterX[3] = 0;DisCenterY[3] = -1;
                        DisCenterX[4] = 0; DisCenterY[4] = -2;
                        break;
                    case 11:
                        DisCenterX[0] = 0;DisCenterY[0] = 0;
                        DisCenterX[1] = 0;DisCenterY[1] = 1;
                        DisCenterX[2] = 0;DisCenterY[2] = 2;
                        DisCenterX[3] = 0;DisCenterY[3] = -1;
                        DisCenterX[4] = -1; DisCenterY[4] = -1;
                        break;
                    case 12:
                        DisCenterX[0] = 0;DisCenterY[0] = 0;
                        DisCenterX[1] = 0;DisCenterY[1] = 1;
                        DisCenterX[2] = 0;DisCenterY[2] = 2;
                        DisCenterX[3] = 0;DisCenterY[3] = -1;
                        DisCenterX[4] = -1; DisCenterY[4] = 0;
                        break;
                    case 13:
                        DisCenterX[0] = 0;DisCenterY[0] = 0;
                        DisCenterX[1] = 0;DisCenterY[1] = 1;
                        DisCenterX[2] = 1;DisCenterY[2] = 0;
                        DisCenterX[3] = 0;DisCenterY[3] = -1;
                        DisCenterX[4] = -1; DisCenterY[4] = 0;
                        break;
                    case 14:
                        DisCenterX[0] = 0;DisCenterY[0] = 0;
                        DisCenterX[1] = 0;DisCenterY[1] = 1;
                        DisCenterX[2] = -1;DisCenterY[2] = 0;
                        DisCenterX[3] = 0;DisCenterY[3] = -1;
                        DisCenterX[4] = -1; DisCenterY[4] = -1;
                        break;
                    case 15:
                        DisCenterX[0] = 0;DisCenterY[0] = 0;
                        DisCenterX[1] = 0;DisCenterY[1] = 1;
                        DisCenterX[2] = 0;DisCenterY[2] = -1;
                        DisCenterX[3] = -1;DisCenterY[3] = -1;
                        DisCenterX[4] = -1; DisCenterY[4] = 1;
                        break;
                    case 16:
                        DisCenterX[0] = 0;DisCenterY[0] = 0;
                        DisCenterX[1] = 0;DisCenterY[1] = 1;
                        DisCenterX[2] = 0;DisCenterY[2] = -1;
                        DisCenterX[3] = -1;DisCenterY[3] = 0;
                        DisCenterX[4] = -2; DisCenterY[4] = 0;
                        break;
                    case 17:
                        DisCenterX[0] = 0;DisCenterY[0] = 0;
                        DisCenterX[1] = 0;DisCenterY[1] = 1;
                        DisCenterX[2] = 0;DisCenterY[2] = -1;
                        DisCenterX[3] = 1;DisCenterY[3] = 0;
                        DisCenterX[4] = -1; DisCenterY[4] = -1;
                        break;
                    case 18:
                        DisCenterX[0] = 0;DisCenterY[0] = 0;
                        DisCenterX[1] = 0;DisCenterY[1] = 1;
                        DisCenterX[2] = 0;DisCenterY[2] = -1;
                        DisCenterX[3] = 1;DisCenterY[3] = 1;
                        DisCenterX[4] = -1; DisCenterY[4] = -1;
                        break;
                    case 19:
                        DisCenterX[0] = 0;DisCenterY[0] = 0;
                        DisCenterX[1] = 1;DisCenterY[1] = 0;
                        DisCenterX[2] = 0;DisCenterY[2] = -1;
                        DisCenterX[3] = 1;DisCenterY[3] = 1;
                        DisCenterX[4] = -1; DisCenterY[4] = -1;
                        break;
                    case 20:
                        DisCenterX[0] = 0;DisCenterY[0] = 0;
                        DisCenterX[1] = 0;DisCenterY[1] = 1;
                        DisCenterX[2] = 0;DisCenterY[2] = 2;
                        DisCenterX[3] = -1;DisCenterY[3] = 0;
                        DisCenterX[4] = -1; DisCenterY[4] = -1;
                        break;
                    case 21:
                        DisCenterX[0] = 0;DisCenterY[0] = 0;
                        DisCenterX[1] = 0;DisCenterY[1] = 1;
                        DisCenterX[2] = 0;DisCenterY[2] = 2;
                        DisCenterX[3] = -1;DisCenterY[3] = 0;
                        DisCenterX[4] = -2; DisCenterY[4] = 0;
                        break;
                }
                //棋子中心点,从中心点扩展标记棋子
                for ( int qzx = x - 2; qzx <= x + 2; qzx++ ) {
                    for ( int qzy = y - 2; qzy <= y + 2; qzy++ ) {
                        //console.log('中心点'+qzx+' '+qzy);
                        if ( qzx >= 1 && qzx <= 20 && qzy >= 1 && qzy <= 20 ) { //中心点合法

                            //111111111原来棋子的方向顺时针旋转
                            for ( int direct = 1; direct <= 8; ++direct ) {
                                if ( direct == 5 ) { //左右旋转尝试
                                    int[] tempZ;
                                    tempZ = DisCenterX;
                                    DisCenterX = LeftAndRightX(DisCenterX,DisCenterY);
                                    DisCenterY = LeftAndRightY(tempZ,DisCenterY);//旋转90度
                                }

                                int[] tempZ = new int[5];
                                tempZ = DisCenterX;
                                DisCenterX = Xuan90X(DisCenterX,DisCenterY);
                                DisCenterY = Xuan90Y(tempZ,DisCenterY);//旋转90度

                                boolean flagGai = false;//是否覆盖选择的x,y点
                                int illegal = 0;//中心点扩展棋子违规的点的数量


                                for (int DCXi = 0; DCXi < DisCenterX.length; ++DCXi ) {
                                    int tempx = qzx + DisCenterX[DCXi];
                                    int tempy = qzy + DisCenterY[DCXi];
                                    //console.log(tempx + ' ' + tempy);
                                    if ( tempx >= 1 && tempx <= 20 && tempy >= 1 && tempy <= 20 ) {
                                        if ( tempx == x && tempy == y ) {
                                            flagGai = true;
                                        }
                                        if ( boxIndex[tempx][tempy] == "0" || boxIndex[tempx][tempy] == canPosition[no]) {
                                            if (
                                                    (tempy+1 > 20 ||(tempy + 1 <= 20 && (boxIndex[tempx][tempy+1] == "0" ||
                                                            (boxIndex[tempx][tempy+1] != piecesColor[no]) ) ) )
                                                            &&
                                                            (tempy-1<1||( tempy - 1 >= 1 && (boxIndex[tempx][tempy-1] == "0" ||
                                                                    (boxIndex[tempx][tempy-1] != piecesColor[no]) )) )
                                                            &&
                                                            (tempx+1>20||( tempx + 1 <= 20 && (boxIndex[tempx+1][tempy] == "0" ||
                                                                    (boxIndex[tempx+1][tempy] != piecesColor[no]) )) )
                                                            &&
                                                            ( tempx-1 < 1||( tempx - 1 >= 1 && (boxIndex[tempx-1][tempy] == "0" ||
                                                                    (boxIndex[tempx-1][tempy] != piecesColor[no]) )) )
                                            ) {
                                                ;
                                            }
                                        }
                                        else {
                                            illegal++;
                                        }
                                    }
                                    else {
                                        illegal++;
                                    }
                                }

                                //能放!!!!!!!!!!!!!!!
                                if ( flagGai && illegal == 0 ) {
                                    for ( int DCXi = 0; DCXi < DisCenterX.length; ++DCXi ) {
                                        int tempx = qzx + DisCenterX[DCXi];
                                        int tempy = qzy + DisCenterY[DCXi];
                                        boxIndex[tempx][tempy] = piecesColor[no];
                                    }
                                    //画上，直接退出
                                    comUsed[no][ComChoose] = true;

                                    // drawChessBoard(boxIndex);
                                    return a;
                                }
                            }

                            //222222222左右翻转后，再顺时针旋转尝试  TODO

                        }
                    }
                }
            }
            //>>>>>>>>>>>>>>>>>>>>>>>>第二部分,如果随机方案不成功，从大到小遍历，谁合适就放谁
            if ( !flag ) {
                for ( ComChoose = 21; ComChoose >= 1; --ComChoose ) {
                    //console.log('选择棋子'+ComChoose)
                    int[] DisCenterX = new int[5];
                    int[] DisCenterY = new int[5];
                    if ( comUsed[no][ComChoose] == false ) {
                        switch(ComChoose) {
                            case 1:
                                DisCenterX[0] = 0;DisCenterY[0] = 0;
                                break;
                            case 2:
                                DisCenterX[0] = 0;DisCenterY[0] = 0;
                                DisCenterX[1] = 0;DisCenterY[1] = 1;
                                break;
                            case 3:
                                DisCenterX[0] = 0;DisCenterY[0] = 0;
                                DisCenterX[1] = 0;DisCenterY[1] = 1;
                                DisCenterX[2] = 0;DisCenterY[2] = -1;
                                break;
                            case 4:
                                DisCenterX[0] = 0;DisCenterY[0] = 0;
                                DisCenterX[1] = 0;DisCenterY[1] = 1;
                                DisCenterX[2] = -1;DisCenterY[2] = 0;
                                break;
                            case 5:
                                DisCenterX[0] = 0;DisCenterY[0] = 0;
                                DisCenterX[1] = 0;DisCenterY[1] = 1;
                                DisCenterX[2] = 0;DisCenterY[2] = 2;
                                DisCenterX[3] = 0;DisCenterY[3] = -1;
                                break;
                            case 6:
                                DisCenterX[0] = 0;DisCenterY[0] = 0;
                                DisCenterX[1] = 0;DisCenterY[1] = 1;
                                DisCenterX[2] = 0;DisCenterY[2] = -1;
                                DisCenterX[3] = -1;DisCenterY[3] = -1;
                                break;
                            case 7:
                                DisCenterX[0] = 0;DisCenterY[0] = 0;
                                DisCenterX[1] = 0;DisCenterY[1] = 1;
                                DisCenterX[2] = 0;DisCenterY[2] = -1;
                                DisCenterX[3] = -1;DisCenterY[3] = 0;
                                break;
                            case 8:
                                DisCenterX[0] = 0;DisCenterY[0] = 0;
                                DisCenterX[1] = -1;DisCenterY[1] = -1;
                                DisCenterX[2] = 0;DisCenterY[2] = -1;
                                DisCenterX[3] = -1;DisCenterY[3] = 0;
                                break;
                            case 9:
                                DisCenterX[0] = 0;DisCenterY[0] = 0;
                                DisCenterX[1] = 0;DisCenterY[1] = 1;
                                DisCenterX[2] = -1;DisCenterY[2] = 0;
                                DisCenterX[3] = -1;DisCenterY[3] = -1;
                                break;
                            case 10:
                                DisCenterX[0] = 0;DisCenterY[0] = 0;
                                DisCenterX[1] = 0;DisCenterY[1] = 1;
                                DisCenterX[2] = 0;DisCenterY[2] = 2;
                                DisCenterX[3] = 0;DisCenterY[3] = -1;
                                DisCenterX[4] = 0; DisCenterY[4] = -2;
                                break;
                            case 11:
                                DisCenterX[0] = 0;DisCenterY[0] = 0;
                                DisCenterX[1] = 0;DisCenterY[1] = 1;
                                DisCenterX[2] = 0;DisCenterY[2] = 2;
                                DisCenterX[3] = 0;DisCenterY[3] = -1;
                                DisCenterX[4] = -1; DisCenterY[4] = -1;
                                break;
                            case 12:
                                DisCenterX[0] = 0;DisCenterY[0] = 0;
                                DisCenterX[1] = 0;DisCenterY[1] = 1;
                                DisCenterX[2] = 0;DisCenterY[2] = 2;
                                DisCenterX[3] = 0;DisCenterY[3] = -1;
                                DisCenterX[4] = -1; DisCenterY[4] = 0;
                                break;
                            case 13:
                                DisCenterX[0] = 0;DisCenterY[0] = 0;
                                DisCenterX[1] = 0;DisCenterY[1] = 1;
                                DisCenterX[2] = 1;DisCenterY[2] = 0;
                                DisCenterX[3] = 0;DisCenterY[3] = -1;
                                DisCenterX[4] = -1; DisCenterY[4] = 0;
                                break;
                            case 14:
                                DisCenterX[0] = 0;DisCenterY[0] = 0;
                                DisCenterX[1] = 0;DisCenterY[1] = 1;
                                DisCenterX[2] = -1;DisCenterY[2] = 0;
                                DisCenterX[3] = 0;DisCenterY[3] = -1;
                                DisCenterX[4] = -1; DisCenterY[4] = -1;
                                break;
                            case 15:
                                DisCenterX[0] = 0;DisCenterY[0] = 0;
                                DisCenterX[1] = 0;DisCenterY[1] = 1;
                                DisCenterX[2] = 0;DisCenterY[2] = -1;
                                DisCenterX[3] = -1;DisCenterY[3] = -1;
                                DisCenterX[4] = -1; DisCenterY[4] = 1;
                                break;
                            case 16:
                                DisCenterX[0] = 0;DisCenterY[0] = 0;
                                DisCenterX[1] = 0;DisCenterY[1] = 1;
                                DisCenterX[2] = 0;DisCenterY[2] = -1;
                                DisCenterX[3] = -1;DisCenterY[3] = 0;
                                DisCenterX[4] = -2; DisCenterY[4] = 0;
                                break;
                            case 17:
                                DisCenterX[0] = 0;DisCenterY[0] = 0;
                                DisCenterX[1] = 0;DisCenterY[1] = 1;
                                DisCenterX[2] = 0;DisCenterY[2] = -1;
                                DisCenterX[3] = 1;DisCenterY[3] = 0;
                                DisCenterX[4] = -1; DisCenterY[4] = -1;
                                break;
                            case 18:
                                DisCenterX[0] = 0;DisCenterY[0] = 0;
                                DisCenterX[1] = 0;DisCenterY[1] = 1;
                                DisCenterX[2] = 0;DisCenterY[2] = -1;
                                DisCenterX[3] = 1;DisCenterY[3] = 1;
                                DisCenterX[4] = -1; DisCenterY[4] = -1;
                                break;
                            case 19:
                                DisCenterX[0] = 0;DisCenterY[0] = 0;
                                DisCenterX[1] = 1;DisCenterY[1] = 0;
                                DisCenterX[2] = 0;DisCenterY[2] = -1;
                                DisCenterX[3] = 1;DisCenterY[3] = 1;
                                DisCenterX[4] = -1; DisCenterY[4] = -1;
                                break;
                            case 20:
                                DisCenterX[0] = 0;DisCenterY[0] = 0;
                                DisCenterX[1] = 0;DisCenterY[1] = 1;
                                DisCenterX[2] = 0;DisCenterY[2] = 2;
                                DisCenterX[3] = -1;DisCenterY[3] = 0;
                                DisCenterX[4] = -1; DisCenterY[4] = -1;
                                break;
                            case 21:
                                DisCenterX[0] = 0;DisCenterY[0] = 0;
                                DisCenterX[1] = 0;DisCenterY[1] = 1;
                                DisCenterX[2] = 0;DisCenterY[2] = 2;
                                DisCenterX[3] = -1;DisCenterY[3] = 0;
                                DisCenterX[4] = -2; DisCenterY[4] = 0;
                                break;
                        }

                        //棋子中心点,从中心点扩展标记棋子
                        for ( int qzx = x - 2; qzx <= x + 2; qzx++ ) {
                            for ( int qzy = y - 2; qzy <= y + 2; qzy++ ) {
                                //console.log('中心点'+qzx+' '+qzy);
                                if ( qzx >= 1 && qzx <= 20 && qzy >= 1 && qzy <= 20 ) { //中心点合法
                                    //1111111111原来棋子的方向顺时针旋转
                                    for ( int direct = 1; direct <= 4; ++direct ) {
                                        int tempZ[] = new int[5];
                                        tempZ = DisCenterX;
                                        DisCenterX = Xuan90X(DisCenterX,DisCenterY);
                                        DisCenterY = Xuan90Y(tempZ,DisCenterY);//旋转90度

                                        boolean flagGai = false;//是否覆盖选择的x,y点
                                        int illegal = 0;//中心点扩展棋子违规的点的数量


                                        for (int DCXi = 0; DCXi < DisCenterX.length; ++DCXi ) {
                                            int tempx = qzx + DisCenterX[DCXi];
                                            int tempy = qzy + DisCenterY[DCXi];
                                            //console.log(tempx + ' ' + tempy);
                                            if ( tempx >= 1 && tempx <= 20 && tempy >= 1 && tempy <= 20 ) {
                                                if ( tempx == x && tempy == y ) {
                                                    flagGai = true;
                                                }
                                                if ( boxIndex[tempx][tempy] == "0" || boxIndex[tempx][tempy] == canPosition[no]) {
                                                    if (
                                                            (tempy+1 > 20 ||(tempy + 1 <= 20 && (boxIndex[tempx][tempy+1] == "0" ||
                                                                    (boxIndex[tempx][tempy+1] != piecesColor[no]) ) ) )
                                                                    &&
                                                                    (tempy-1<1||( tempy - 1 >= 1 && (boxIndex[tempx][tempy-1] == "0" ||
                                                                            (boxIndex[tempx][tempy-1] != piecesColor[no]) )) )
                                                                    &&
                                                                    (tempx+1>20||( tempx + 1 <= 20 && (boxIndex[tempx+1][tempy] == "0" ||
                                                                            (boxIndex[tempx+1][tempy] != piecesColor[no]) )) )
                                                                    &&
                                                                    ( tempx-1 < 1||( tempx - 1 >= 1 && (boxIndex[tempx-1][tempy] == "0" ||
                                                                            (boxIndex[tempx-1][tempy] != piecesColor[no]) )) )
                                                    ) {
                                                        ;
                                                    }
                                                }
                                                else {
                                                    illegal++;
                                                }
                                            }
                                            else {
                                                illegal++;
                                            }
                                        }

                                        //能放!!!!!!!!!!!!!!!
                                        if ( flagGai && illegal == 0 ) {
                                            for ( int DCXi = 0; DCXi < DisCenterX.length; ++DCXi ) {
                                                int tempx = qzx + DisCenterX[DCXi];
                                                int tempy = qzy + DisCenterY[DCXi];
                                                boxIndex[tempx][tempy] = piecesColor[no];
                                            }
                                            //画上，直接退出
                                            comUsed[no][ComChoose] = true;

                                            // drawChessBoard(boxIndex);
                                            return a;
                                        }

                                    }

                                    //222222222左右翻转后，再顺时针旋转尝试  TODO

                                }
                            }
                        }
                    }
                }
            }
            //<<=<<<<<<<<<<<<<<<<<==========
        }
        a[no] = 1;
        return a;
    }
}
